﻿using Alone.Handlers;
using AloneLibrary.GameObjects;
using AloneLibrary.Services.Camera;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNAnimation;
using XNAnimation.Controllers;
using XNAnimation.Effects;

namespace Alone.GameObjects.Objects
{

	public class Handgun : DynamicGameObject
	{
        private IPlayerHandler playerHandler;
		private Game game;
		private int ammo;

        /// <summary>
        /// Gets or sets the ammo.
        /// </summary>
        /// <value>The ammo.</value>
		public int Ammo
		{
			get { return ammo; }
			set { ammo = value; }
		}

        /// <summary>
        /// Initializes a new instance of the <see cref="Handgun"/> class.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <param name="model">The model.</param>
        /// <param name="animationController">The animation controller.</param>
        /// <param name="effect">The effect.</param>
        /// <param name="position">The position.</param>
        /// <param name="rotation">The rotation.</param>
        /// <param name="scale">The scale.</param>
        /// <param name="velocity">The velocity.</param>
		public Handgun(Game game, SkinnedModel model, AnimationController animationController, Effect effect, Vector3 position, Vector2 rotation, float scale, Vector3 velocity)
			: base(model, animationController, effect, position, rotation, scale, velocity)
		{
			this.game = game;
			this.ammo = 20;
            playerHandler = (IPlayerHandler)this.game.Services.GetService(typeof(IPlayerHandler));
		}

        /// <summary>
        /// Draws the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <param name="camera">The camera.</param>
		public override void Draw(GameTime gameTime, ICameraHandler camera)
		{
            if (Enabled)
            {
                Matrix[] transforms = new Matrix[skinnedModel.Model.Bones.Count];
                skinnedModel.Model.CopyAbsoluteBoneTransformsTo(transforms);

                // Since the weapon model doesn't have any animations, we animate it with
                // the help of the players right hand
                world = animationController.GetBoneAbsoluteTransform("R_Hand2")
               * Matrix.CreateRotationY(playerHandler.Player.Rotation.Y)
               * Matrix.CreateTranslation(playerHandler.Player.Position);

                // Drawing the weapon model
                foreach (ModelMesh modelMesh in skinnedModel.Model.Meshes)
                {
                    foreach (SkinnedModelBasicEffect effect in modelMesh.Effects)
                    {
                        // Setup camera
                        effect.View = camera.View;
                        effect.Projection = camera.Projection;
                        // We set the World matrix as the absolute transform of the bone that we want to attach
                        // the object to
                        effect.World = world;

                        // OPTIONAL - Configure material
                        effect.Material.DiffuseColor = new Vector3(0.8f);
                        effect.Material.SpecularColor = new Vector3(0.3f);
                        effect.Material.SpecularPower = 8;

                        // OPTIONAL - Configure lights
                        effect.AmbientLightColor = new Vector3(0.1f);
                        effect.LightEnabled = true;
                        effect.EnabledLights = EnabledLights.One;
                        effect.PointLights[0].Color = Vector3.One;
                        effect.PointLights[0].Position = new Vector3(100);
                    }

                    // Draw a model mesh
                    modelMesh.Draw();
                }
            }
		}
	}
}
